package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.dao.User;
import com.company.project.dao.UserDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDao userDao;

    @PostMapping("/login")
    public Map<String,Object> page(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        User data = userDao.login(user);
        if (data == null){
            map.put("code", 0);
            map.put("message","登录失败");
            return map;
        } else {
            map.put("code", 200);
            map.put("message","成功");
            return map;
        }
    }

    @GetMapping("/page")
    public Map<String, Object> page(Integer page, Integer size,String name) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, size);
        List<User> list = userDao.findAll(name);
        PageInfo pageInfo = new PageInfo(list);
        map.put("code", 200);
        map.put("message","成功");
        map.put("data", pageInfo);
        return map;
    }

    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        userDao.save(user);
        map.put("code", 200);
        map.put("message","成功");
        return map;
    }

    @GetMapping("/deleteById")
    public Map<String, Object> deleteById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        userDao.deleteById(id);
        map.put("code", 200);
        map.put("message","成功");
        return map;
    }

    @PostMapping("/update")
    public Map<String, Object> update(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        userDao.update(user);
        map.put("code", 200);
        map.put("message","成功");
        return map;
    }

    @GetMapping("/findById")
    public Map<String, Object> findById(@RequestParam Integer id) {
        Map<String, Object> map = new HashMap<>();
        User user = userDao.findById(id);
        map.put("code", 200);
        map.put("message","成功");
        map.put("data", user);
        return map;
    }

}
